package com.example.util;

import com.example.cache.MemoryCache;
import com.example.pojo.Location;
import com.example.pojo.LogVO;
import com.example.pojo.Mission;
import com.example.pojo.MissionVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class ContextUtil {

    @Resource
    private MemoryCache cache;

    /**
     * 将生成的巡航途径点转为上下文记忆
     * @param missionVO 巡航参数
     * @return 文字记忆
     */
    public void memoryLocations(Mission mission, MissionVO missionVO){
        if(missionVO.getLocations().isEmpty()){
            return;
        }
        StringBuilder res = new StringBuilder("巡航路线已规划:\n");
        int n = 1;
        for(Location item : missionVO.getLocations()){
            res.append("第").append(n).append("个途径点经纬度位置: ").append(item.getLongitude()).append(",").append(item.getLatitude()).append("\n");
        }
        cache.append(mission.getId(), res.toString(), mission.getTime());
    }

    public void memoryLogs(Mission mission, LogVO log){
        if(log.isEmpty()){
            return;
        }
        StringBuilder res = new StringBuilder("巡航任务日志:\n");
        res.append(log.getInfo()).append("\n");
        cache.append(mission.getId(), res.toString(), mission.getTime());
    }

    public String getLogs(Mission mission){
        if(!cache.containsKey(mission.getId())){
            return "";
        }
        return cache.get(mission.getId()) + "\n----------\n以上为日志记录\n";
    }
}
